$(document).ready(function() {

    $("#nip_ganti").keyup(function() {
        var val=$('#nip_ganti').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nama_ganti').innerHTML=data.nama;
                }else{
                    document.getElementById('nama_ganti').innerHTML='NIP Tidak Ada di Daftar';
                }
                
            });
        }   
    });
    $("#nip_atasan").keyup(function() {
        var val=$('#nip_atasan').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nama_langsung').innerHTML=data.nama;
                }else{
                    document.getElementById('nama_langsung').innerHTML='NIP Tidak Ada di Daftar';
                }
                
            });
        }   
    });
    $("#nip_setuju").keyup(function() {
        var val=$('#nip_setuju').val();
        if(val.length===18){
            $.getJSON('controller/cont.pegawai.php',{aksi_biodata:true,nip:val},function(data){
                if(data.msg){
                    document.getElementById('nama_setuju').innerHTML=data.nama;
                }else{
                    document.getElementById('nama_setuju').innerHTML='NIP Tidak Ada di Daftar';
                }
                
            });
        }   
    });

    $.getJSON('controller/cont.cuti.php',{get_ref_cuti:true},function(data){
        $.each(data,function(index,data){
            var option2 = $('<option />');
            option2.attr('value', data.kode).text(data.uraian); 
            $('#idJns').append(option2);
        });
    });
    $("#btn_simpan").click(function(){
        $.post('controller/cont.cuti.php',$("#frm_cuti").serialize(),function(data){
            if (data.msg) {
                if (data.edit) {
                    goAdd();

                    var ddl = document.getElementById('idJns');
                    var opts = ddl.options.length;
                    for (var i = 0; i < opts; i++) {
                        if (ddl.options[i].value == data.cuti.idJnsCuti) {
                            ddl.options[i].selected = true;
                            break;
                        }
                    }

                    document.getElementById("tgl_awal").value = formattedDate(data.cuti.tglAwal);
                    document.getElementById("tgl_akhir").value = formattedDate(data.cuti.tglAkhir);
                    document.getElementById("nip_ganti").value = data.cuti.nipGanti;
                    document.getElementById("nip_atasan").value = data.cuti.nipAtasan;
                    document.getElementById("nip_setuju").value = data.cuti.nipSetuju;
                    document.getElementById("alamat").value = data.cuti.alamat;
                    document.getElementById("telp").value = data.cuti.telp;
                    document.getElementById("frm_aksi").value = "simpan_edit";
                    document.getElementById("saveButton").className = "buttonshort";
                    document.getElementById("undoButton").className = "buttonshort";
                    document.getElementById("hapusButton").className = "buttonshortdis";
                    document.getElementById("editButton").className = "buttonshortdis";
                } else {
                    document.getElementById("sukses").innerHTML = data.sukses;
                    $("#div_sukses").fadeIn(500).delay(3500).fadeOut(500);
                    loadCuti();
					goUndo();
                }
            } else {
                document.getElementById("error").innerHTML = data.eror;
                $("#div_error").fadeIn(500).delay(3500).fadeOut(500);
                goUndo();
            }
            return false;
        },'json');
        return false;
    });

    loadCuti();

    $.getJSON('controller/cont.cuti.php', {aksi_cuti_ambil: true}, function(data) {

        $('#list_cuti_ambil tbody').html('');
        var tab = '';

        if (data.msg) {
            $.each(data.ambil, function(index, ambil) {
                tab += "<tr>";
                tab += "<td>" + ambil.cuti.tglAju + "</td>";
                tab += "<td>" + ambil.cuti.tglAwal + " s.d. " + ambil.cuti.tglAkhir + "</td>";
                tab += "<td>" + ambil.cuti.jmlHari + "</td>";
                tab += "<td>" + ambil.ref + "</td>";
                tab += "</tr>";
            });
            $('#list_cuti_ambil tbody').append(tab);
        } else {
            tab += "<tr>"
            tab += "<td>Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_cuti_ambil tbody').append(tab);
        }
    });
    
    $.getJSON('controller/cont.cuti.php', {aksi_cuti_batal: true}, function(data) {

        $('#list_cuti_batal tbody').html('');
        var tab = '';

        if (data.msg) {
            $.each(data.batal, function(index, batal) {
                tab += "<tr>";
                tab += "<td>" + batal.detail.tglBatal + "</td>";
                tab += "<td>" + batal.cutiBatal.tglAwal + " s.d. " + batal.cutiBatal.tglAkhir + "</td>";
                tab += "<td>" + batal.detail.tglAwal + " s.d. " + batal.detail.tglAkhir + "</td>";
                tab += "<td>" + batal.ref + "</td>";
                tab += "<td>" + batal.cutiBatal.jmlHari + "</td>";
                tab += "<td>" + batal.detail.jmlHariBatal + "</td>";
                tab += "<td>" + (parseInt(batal.cutiBatal.jmlHari)-parseInt(batal.detail.jmlHariBatal))+ "</td>";
                tab += "<td>" + batal.detail.Alasan + "</td>";
                tab += "</tr>";
            });
            $('#list_cuti_batal tbody').append(tab);
        } else {
            tab += "<tr>"
            tab += "<td>Data Tidak Ada</td>";
            tab += "</tr>";
            $('#list_cuti_batal tbody').append(tab);
        }
    });


    return false;
});




function formattedDate(date) {
    var d = new Date(date || Date.now()),
        month = '' + (d.getMonth() + 1),
        day = '' + d.getDate(),
        year = d.getFullYear();

    if (month.length < 2) month = '0' + month;
    if (day.length < 2) day = '0' + day;

    return [day, month, year].join('-');
}

function loadCuti(){
    $.getJSON('controller/cont.cuti.php', {aksi_cuti: true}, function(data) {
        $('#list_cuti_aju tbody').html('');
        $('#list_cuti_sedang tbody').html('');
        var tab = '';
        tab += "<tr>"
        tab += "<td>Data Tidak Ada</td>";
        tab += "</tr>";

        var tab_aju = '';
        var tab_sedang = '';
        if (data.msg) {
            if (data.aju!==false) {
                $.each(data.aju, function(index, aju) {
                    tab_aju += "<tr>";
                    tab_aju += "<td>" + aju.cuti.tglAju + "</td>";
                    tab_aju += "<td>" + aju.cuti.tglAwal + " s.d. " + aju.cuti.tglAkhir + "</td>";
                    tab_aju += "<td>" + aju.cuti.jmlHari + "</td>";
                    tab_aju += "<td>" + aju.ref + "</td>";
                    tab_aju += "<td align=\"center\"><input id=\"selectLibur\" name=\"selectLibur\" type=\"radio\" value=\"" + aju.cuti.id + "\"/></td>";
                    tab_aju += "</tr>";
                });
                $('#list_cuti_aju tbody').append(tab_aju);
            } else {
                $('#list_cuti_aju tbody').append(tab);
            }

            if (data.sedang!==false) {
                $.each(data.sedang, function(index, sedang) {
                    tab_sedang += "<tr>";
                    tab_sedang += "<td>" + sedang.cuti.tglAju + "</td>";
                    tab_sedang += "<td>" + sedang.cuti.tglAwal + " s.d. " + sedang.cuti.tglAkhir + "</td>";
                    tab_sedang += "<td>" + sedang.cuti.jmlHari + "</td>";
                    tab_sedang += "<td>" + sedang.ref + "</td>";
                    tab_sedang += "</tr>";
                });
                $('#list_cuti_sedang tbody').append(tab_sedang);
            } else {
                $('#list_cuti_sedang tbody').append(tab);
            }
        } else {
            $('#list_cuti_aju tbody').append(tab);
            $('#list_cuti_sedang tbody').append(tab);
        }
    });
}

